//
// Created by 吴喆 on 2020/3/30.
//

#include "DemoBulletParticleGenerator.h"
#include "../engine/Bullet.h"
#include "automation/BulletStateMachine.h"

DemoBulletParticleGenerator::DemoBulletParticleGenerator(Texture2D *texture, Playable *parent, int count)
        : ParticleGenerator(texture, parent, count) {

}

void DemoBulletParticleGenerator::update() {

    auto *bullet = dynamic_cast<Bullet *>(parent);
    if (bullet) {
        if (typeid(*(bullet->machines[0]->currentState)) == typeid(BulletOnTheWayState)) {
            // Update all particles
            ParticleGenerator::update();
        } else {
            for (unsigned int i = 0; i < particleCount; ++i) {
                Particle &p = particles[i];
                p.life -= CTime::getInstance()->getDelta();
                if (p.life > 0.0f) {   // particle is alive, thus update
                    p.position = glm::vec2(p.position.x,
                                           p.position.y + p.velocity.y * CTime::getInstance()->getDelta());
                    p.color.w -= CTime::getInstance()->getDelta() * 2.5;
                }
            }

        }
    }
}

